const tradeService = require('./../services/trade.service');

module.exports = {
    /**
     * 交易记录添加
     */
    async addTradeRecord(ctx) {
        let result = {
            result: false,
            message: ''
        };
        let formData = ctx.request.body;
        let res = await tradeService.addTradeRecord({
            buyer_id: formData.buyer_id,
            seller_id: formData.seller_id,
            book_id: formData.book_id
        });
        if (res && res.affectedRows) {
            result.result = true;
            result.message = '交易记录添加成功';
        }
        ctx.body = result;
    },
    /**
     * 出售记录获取
     * 
     */
    async getSaleRecord(ctx) {
        let result = {
            result: false,
            message: '',
            records: []
        };
        let user_id = ctx.query.user_id;
        let res = await tradeService.getSaleRecord({ user: user_id });
        result.result = true;
        result.records = res;
        ctx.body = result;
    },
    /**
     * 购买记录获取
     */
    async getPurchaseRecord(ctx) {
        let result = {
            result: false,
            message: '',
            records: []
        };
        let user_id = ctx.query.user_id;
        let res = await tradeService.getPurchaseRecord({ user: user_id });
        result.result = true;
        result.records = res;
        ctx.body = result;
    }
}